package com.nd.mobile.study.view;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.nd.mobile.study.R;
import com.nd.mobile.study.common.BaseActivity;
import com.nd.mobile.study.common.constants.BundleConstants;
import com.nd.mobile.study.common.constants.FlagConstants;

public class AppActivity extends BaseActivity {

	private TextView 		mAppTitle;
	private GridView		mAppGV;
	private List<AppDisp>	mAppDisps;

	@Override
	protected void onBaseCreate(Bundle savedInstanceState) {
		setContentView(R.layout.app);
	}

	@Override
	protected void onAfterCreate(Bundle savedInstanceStat) {
		mAppGV.setAdapter(new AppGVAdapter(this, mAppDisps));
	}

	@Override
	protected void setFieldValues() {
		super.setFieldValues();
		// demo
		mAppDisps = new ArrayList<AppDisp>();
		mAppDisps.add(new AppDisp(R.drawable.ico_01, R.string.tv_app_myclass));
		mAppDisps.add(new AppDisp(R.drawable.ico_02, R.string.tv_app_recentnotify));
		mAppDisps.add(new AppDisp(R.drawable.ico_03, R.string.tv_app_video));
		mAppDisps.add(new AppDisp(R.drawable.ico_04, R.string.tv_app_studydocu));
		mAppDisps.add(new AppDisp(R.drawable.ico_05, R.string.tv_app_courseexercise));
		mAppDisps.add(new AppDisp(R.drawable.ico_05, R.string.tv_app_courseexam));
		mAppDisps.add(new AppDisp(R.drawable.ico_06, R.string.tv_app_studyrecord));
	}

	@Override
	protected void initViews() {
		super.initHeader();
		mHeaderTitleTv.setText(R.string.app_name);
		mHeaderBtnBack.setVisibility(View.GONE);
		
		mAppTitle = (TextView)findViewById(R.id.tv_list_title);
		mAppTitle.setText("海西教育网");
		
		mAppGV = (GridView) findViewById(R.id.app_grid);
		mAppGV.setSelector(new ColorDrawable(Color.TRANSPARENT));
		mAppGV.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				if(AppActivity.this.checkLogin() == false){
					return;
				}
				boolean isMatched = true;
				Intent intent = new Intent();
				Class<? extends Activity> dest = null;
				Class<? extends Activity> nextDest = null;
				Class<? extends Activity> lastDest = null;
				int headerTitleResId = 0;
				switch (position) {
				case FlagConstants.AppFrom.MY_CLASS:
					headerTitleResId = R.string.tv_app_myclass;
					dest = ClassListActivity.class;
					nextDest = ClassInfoCatalogActivity.class;
					break;
				case FlagConstants.AppFrom.RECENT_ATTENTION:
					dest = AttentionCatalogActivity.class;
					break;
				case FlagConstants.AppFrom.VIDEO:
					headerTitleResId = R.string.tv_app_video;
					dest = ClassListActivity.class;
					nextDest = SubjcetActivity.class;
					lastDest = VideoCatalogActivity.class;
					break;
				case FlagConstants.AppFrom.MATERIAL:
					headerTitleResId = R.string.tv_app_studydocu;
					dest = ClassListActivity.class;
					nextDest = SubjcetActivity.class;
					lastDest = MaterialInfoCatalogActivity.class;
					break;
				case FlagConstants.AppFrom.HOMEWORK:
					headerTitleResId = R.string.tv_app_courseexercise;
					dest = ClassListActivity.class;
					nextDest = SubjcetActivity.class;
					lastDest = HomeworkInfoCatalogActivity.class;
					break;
				case FlagConstants.AppFrom.ROLLSET:
					headerTitleResId = R.string.tv_app_courseexam;
					dest = ClassListActivity.class;
					nextDest = SubjcetActivity.class;
					lastDest = BlueBookCatalogActivity.class;
					break;
				case FlagConstants.AppFrom.RECODE:
					dest = StudyRecordTabActivity.class;
					break;
				default:
					isMatched = false;
					break;
				}
				
				if(isMatched) {
					intent
					.setClass(AppActivity.this, dest)
					.putExtra(BundleConstants.HEADER_TITLE, headerTitleResId)
					.putExtra(BundleConstants.NEXT_DEST, nextDest)
					.putExtra(BundleConstants.LAST_DEST, lastDest);
					startActivity(intent);
				}
			}
		});
	}

	private class AppDisp {
		private int	picResId;
		private int	strResId;

		public AppDisp(int picResId, int strResId) {
			super();
			this.picResId = picResId;
			this.strResId = strResId;
		}
	}

	private static class AppGVAdapter extends BaseAdapter {
		private final static class ViewHolder {
			ImageView	iv;
			TextView	tv;
		}

		private List<AppDisp>	mData;
		private LayoutInflater	inflater;

		public AppGVAdapter(Context context, List<AppDisp> appDisps) {
			super();
			this.mData = appDisps;
			inflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount() {
			return null == mData ? 0 : mData.size();
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (null == convertView) {
				convertView = inflater.inflate(R.layout.app_grid_item, null);

				holder = new ViewHolder();
				holder.iv = (ImageView) convertView.findViewById(R.id.app_grid_icon);
				holder.tv = (TextView) convertView.findViewById(R.id.app_grid_name);

				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			holder.tv.setText(mData.get(position).strResId);
			holder.iv.setImageResource(mData.get(position).picResId);

			return convertView;
		}
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		boolean flag = (keyCode == KeyEvent.KEYCODE_BACK);
        if (flag) {
            showExitDialog();
        }
        return flag;
	}
}
